package com.hzdesk.media.koreabatchmatvector.dao;

import android.content.Context;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.bean.AdvertisingBean;
import com.hzdesk.media.koreabatchmatvector.bean.AdvertisingBeanDao;

import java.util.List;

/**
 * Created by Administrator on 2018/7/2.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class AdvertisingBeanDaoUtils {
    private static final String TAG = AdvertisingBeanDaoUtils.class.getSimpleName();
    private DaoManager daoManager;

    public AdvertisingBeanDaoUtils(Context context) {
        daoManager = DaoManager.getInstance();
        daoManager.init(context);
    }
    /**
     * @desc 插入数据
     * */
    public boolean insertAdvertising(AdvertisingBean bean){
        boolean flag = false;
        AdvertisingBeanDao dao = daoManager.getDaoSession().getAdvertisingBeanDao();
        try {
            boolean  flag_01 = isRepeat(bean, dao);
            if(!flag_01){
                flag = dao.insertOrReplace(bean) == -1? false:true;
            }
        } catch (Exception e) {

        }
        return flag;
    }

    private boolean isRepeat(AdvertisingBean bean, AdvertisingBeanDao dao) {
        boolean flag = false;
        try {
            List<AdvertisingBean> user1s =  dao.loadAll();
            for (AdvertisingBean user1 : user1s) {
               String id =  user1.getId();
               if(id.equals(bean.getId())){
                   flag = true;
               }
            }
        } catch (Exception e) {

        }
        return flag;
    }

    public List<AdvertisingBean>  query(long time){
        AdvertisingBeanDao dao = daoManager.getDaoSession().getAdvertisingBeanDao();
        List<AdvertisingBean> bean = null;
        try {
            bean = dao.queryBuilder().where(AdvertisingBeanDao.Properties.StartTime.lt(time),AdvertisingBeanDao.Properties.EndTime.ge(time)).list();
        } catch (Exception e) {

        }
     //   Log.e("SplashPersenter ", "query: "+bean.size() );
        return bean;
    }
}
